package edu.uoc.tdp.pac4.client.logandreg;


import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;


import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;



/**
 * On this window students do new registrations
 *  
 * @param connectionHelper
 *
 */
public class Registration_NewRegistration extends JFrame {
	

	private static final long serialVersionUID = 1L;
	
	private JButton btnCancel= null;
	private JButton btnAccept = null;
	private JComboBox cmbCourses = null;
	private JComboBox cmbGroups = null;
	private JLabel lblStartDate = null;
	private JLabel lblEndDate = null;
	private JLabel lblStartDateText = null;
	private JLabel lblEndDateText = null;
	private HashMap<String, Group> mapGroups = new HashMap<String, Group>();
	
	// Connection to the services
	ClientConnectionHelper connectionHelper_ = null;
	
	

	public Registration_NewRegistration(ClientConnectionHelper connectionHelper) {
		setResizable(false);
		
		this.setMinimumSize(new Dimension(453, 304)); 
		setAlwaysOnTop(true);
		setTitle(LanguageUtils.getMessage("client.regandlog.title.newreg"));
		
		connectionHelper_ = connectionHelper;
		
		
		JLabel lblSelleccionarCurs = new JLabel(LanguageUtils.getMessage("client.regandlog.label.selectcourse"));
		
		JLabel lblSelleccionarGrup = new JLabel(LanguageUtils.getMessage("client.regandlog.label.selectgroup"));
		
		final HashMap<String, Course> map = new NewRegistrationHelper().getMap(connectionHelper_);
		
		
		btnCancel  = new JButton(LanguageUtils.getMessage("client.button.cancel"));
		btnAccept = new JButton(LanguageUtils.getMessage("client.button.accept"));
		
		lblStartDate = new JLabel(LanguageUtils.getMessage("client.regandlog.label.startdate"));
		lblStartDate.setVisible(false);
		lblEndDate = new JLabel(LanguageUtils.getMessage("client.regandlog.label.startdate"));
		lblEndDate.setVisible(false);
		lblStartDateText = new JLabel("New label");
		lblStartDateText.setVisible(false);
		lblEndDateText = new JLabel("New label");
		lblEndDateText.setVisible(false);
		
		//Filling combo Courses 
		cmbGroups = new JComboBox();
		cmbCourses = new JComboBox();
		cmbGroups.setEnabled(false);
		cmbCourses.setBounds(167, 26, 120, 20);
		cmbCourses.setModel(new NewRegistrationHelper().getComboCoursesModel(connectionHelper_));
		
		//Check if this course has active groups, enables the combo if it's true and fills it
		cmbCourses.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
            	cmbGroups.setEnabled(false);
                lblStartDate.setVisible(false);
                lblStartDateText.setVisible(false);
                lblEndDate.setVisible(false);
                lblEndDateText.setVisible(false);                           
                mapGroups.clear();
                
                JComboBox cmbCourses = (JComboBox)e.getSource();
                String key = (String)cmbCourses.getSelectedItem();
                Course course = map.get(key);
               
                //Show the details of the selected group
                if(new NewRegistrationHelper().getComboGroupsModel(connectionHelper_, course).equals(null))
                	cmbCourses.addItem("No grups");
                else{	          	
                	cmbGroups.setModel(new NewRegistrationHelper().getComboGroupsModel(connectionHelper_, course));
                	mapGroups = new NewRegistrationHelper().getMapGroups(connectionHelper_, course);
                	if(!mapGroups.isEmpty()){
                		cmbGroups.setEnabled(true);
                		cmbGroups.addActionListener(new ActionListener(){
                		public void actionPerformed(ActionEvent e) {
	                		JComboBox cmbGroups = (JComboBox)e.getSource();
	                        String key = (String)cmbGroups.getSelectedItem();
	                        Group group = mapGroups.get(key); 
	                        lblStartDateText.setText(group.getStartDate().toString());
	                        lblEndDateText.setText(group.getFinalDate().toString());
	                        lblStartDate.setVisible(true);
	                        lblStartDateText.setVisible(true);
	                        lblEndDate.setVisible(true);
	                        lblEndDateText.setVisible(true);                           
                   		 }
                		});
                	}
                	else{
                		lblStartDateText.setText(LanguageUtils.getMessage("client.regandlog.label.nogroups"));
                		lblStartDateText.setVisible(true);
                	}
                }
                
            }
		});
	
		

		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnCancel))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblSelleccionarCurs)
								.addComponent(lblSelleccionarGrup))))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(203)
							.addComponent(btnAccept))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(31)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(cmbGroups, Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(cmbCourses, Alignment.LEADING, 0, 285, Short.MAX_VALUE))))
					.addContainerGap(26, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(197, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblStartDate)
						.addComponent(lblEndDate))
					.addGap(51)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblStartDateText)
						.addComponent(lblEndDateText))
					.addGap(83))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelleccionarCurs)
						.addComponent(cmbCourses, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelleccionarGrup)
						.addComponent(cmbGroups, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblStartDate)
						.addComponent(lblStartDateText))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEndDate)
						.addComponent(lblEndDateText))
					.addPreferredGap(ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAccept)
						.addComponent(btnCancel))
					.addGap(27))
		);
		getContentPane().setLayout(groupLayout);
		
		//Accept the registration, check if is possible
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnAccept) {
					try {	
						if(!mapGroups.isEmpty()){
					//		JComboBox cmbGroups = (JComboBox)e.getSource();
	                        String key = (String)cmbGroups.getSelectedItem();
	                        Group group = mapGroups.get(key); 
							new NewRegistrationHelper().registerNewReg(connectionHelper_, group);
									dispose();
						}
						else{
							JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.regandlog.warning.noselectedgroup"),
									LanguageUtils.getMessage("client.regandlog.warning.title"),JOptionPane.WARNING_MESSAGE);
						}
					}catch(Exception e2) {
							JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
									LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
					}
		
				}
			}
		}
		);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnCancel) {
					try {	
						dispose();				
					}catch(Exception e2) {
							JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
									LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
					}
		
				}
			}
		}
		);	
	}
}

